package huiji;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Enemy extends AttackObj {
    static public int showFrequency = 200;//敌人登场频率：200毫秒
    static public long lastCreateTime;//上次创建敌人时间
    static public BufferedImage enemyImage = ImageUtil.gainImage("airplane.png");
    static public List<Enemy> enemies = new ArrayList<>();

    /**
     * 创建敌人
     * @return
     */
    static public void action(){
        //现有敌机挪动或越界移除
        if(enemies.size()>0){
            for (int i =enemies.size()-1;i>=0;i-- ){
                Enemy b = enemies.get(i);
                if(b.isOut()){//子弹越界就删除
                    enemies.remove(i);
                }else{
                    b.setY(b.getY()+b.getSpeed());
                }
            }
        }
        //System.out.println("敌机越界处理剩余数量："+enemies.size());

        final long now = System.currentTimeMillis();
        if(now>lastCreateTime+showFrequency){
            //生成敌机
            final Enemy enemy = new Enemy().setImageAndWH(enemyImage);
            //随机生成敌机的x坐标
            int x = new Random().nextInt(HuijiJPanel.width-enemy.getWidth());
            enemies.add(
                    enemy.setY(0-enemy.getHeight()).setX(x)
            );
            //System.out.println("敌机数量："+enemies.size());
            lastCreateTime = now;
        }


    }

    static public void paintEnemy(Graphics g){
        if (enemies.size()==0) return;
        enemies.stream().forEach(e->{e.paint(g);});
    }

    public Enemy(){
        setSpeed(2);
    }


    @Override
    public Enemy setImage(BufferedImage image) {
        return (Enemy)super.setImage(image);
    }

    @Override
    public Enemy setImageAndWH(BufferedImage image) {
        return (Enemy)super.setImageAndWH(image);
    }

    @Override
    public Enemy setWidth(int width) {
        return (Enemy)super.setWidth(width);
    }

    @Override
    public Enemy setHeight(int height) {
        return (Enemy)super.setHeight(height);
    }

    @Override
    public Enemy setX(int x) {
        return (Enemy)super.setX(x);
    }

    @Override
    public Enemy setY(int y) {
        return (Enemy)super.setY(y);
    }

    @Override
    public Enemy setSpeed(int speed) {
        return (Enemy)super.setSpeed(speed);
    }

    @Override
    public Enemy setImageList(List<HuijiImage> imageList) {
        return (Enemy)super.setImageList(imageList);
    }

    @Override
    public Enemy setIndex(int index) {
        return (Enemy)super.setIndex(index);
    }
}
